const action = require('../action');
const apiPrefix = require('../config').apiPrefix;
const APIError = require('../module/rest').APIError;
const maxPageObj = require('../config').maxPageObj || 25;


module.exports = {
    'GET /api/delivery/:deliveryName': async (ctx, next) => {
        let returnData = await action.delivery.getByName(ctx.params.deliveryName);
        if (returnData.result) {
            ctx.rest({
                "delivery": returnData.data
            });
        } else {
            throw new APIError("delivery:not_found", "delivery not found by id.");
        }
    },

    'POST /api/delivery': async (ctx, next) => {
        let returnData = await action.delivery.create(ctx.request.body.delivery);
        if (returnData.result) {
            ctx.rest({
                "delivery": returnData.data
            });
        } else {
            throw new APIError("delivery:create_failed", "create delivery failed");
        }
    },

    'GET /api/delivery-list': async (ctx, next) => {
        // default page = 1
        let returnData = await action.delivery.getByPage(ctx.request.query.page || 1, maxPageObj);        
        if (returnData.result) {
            ctx.rest(returnData.data);
        } else {
            throw new APIError("delivery:not_found", "delivery not found by id.");
        }
    },

    'GET /api/delivery-count': async (ctx, next) => {
        let returnData = await action.delivery.count();
        if (returnData.result) {
            ctx.rest({
                "count": returnData.data
            });
        } else {
            throw new APIError("delivery:count_fault", "count fault");
        }
    },

    'POST /api/delivery/:deliveryName': async (ctx, next) => {
        let returnData = await action.delivery.createByName(ctx.params.deliveryName);
        if (returnData.result) {
            ctx.rest({
                "delivery": returnData.data
            });
        } else {
            throw new APIError("delivery:create_failed", "create delivery failed");
        }
    },

    "PUT /api/delivery/:deliveryName/state": async (ctx, next) => {
        let returnData = await action.delivery.updateStateByName(ctx.params.deliveryName, ctx.request.body.state);
        if (returnData.result) {
            ctx.rest({
                "result": true
            });
        } else {
            throw new APIError("delivery:update_failed", "delivery not updated");
        }
    }, 

    "PUT /api/delivery/:deliveryName/info": async (ctx, next) => {
        let returnData = await action.delivery.updateInfoByName(ctx.params.deliveryName, ctx.request.body.info);
        if (returnData.result) {
            ctx.rest({
                "result": true
            });
        } else {
            throw new APIError("delivery:update_failed", "delivery not updated");
        }
    }, 

    'DELETE /api/delivery/:deliveryName': async (ctx, next) => {
        let returnData = await action.delivery.deleteByName(ctx.params.deliveryName);
        if (returnData.result) {
            ctx.rest({
                "result": true
            });
        } else {
            throw new APIError("delivery:delete_failed", "delete delivery failed");
        }
    },

};